package com.satis.compose.get.example.pages


import androidx.compose.animation.*
import androidx.compose.foundation.layout.*
import androidx.compose.material.Button
import androidx.compose.material.Surface
import androidx.compose.material.Text
import androidx.compose.runtime.*
import androidx.compose.runtime.livedata.observeAsState
import androidx.compose.ui.Alignment
import androidx.compose.ui.Modifier
import androidx.compose.ui.graphics.Color
import androidx.compose.ui.tooling.preview.Preview
import androidx.compose.ui.unit.dp
import androidx.lifecycle.viewmodel.compose.viewModel
import com.satis.compose.get.example.intent.MainIntent
import com.satis.compose.get.example.viewmodel.MainViewMode
import com.satis.compose.get.navigator.GetPageController
import com.satis.compose.get.navigator.LocalPageController
import com.satis.compose.get.state
import com.satis.compose.get.widget.GetPage

/**
 * @author sunshaobei on 2022/9/24
 */

@OptIn(ExperimentalAnimationApi::class)
@Composable
fun HomePage(navigator: GetPageController = LocalPageController.current) {
    GetPage(darkIcons = true) {
        var content by "content".state
        var index by 0.state
        val viewModel: MainViewMode = viewModel()
        val uiState = viewModel.uiState
        val visible by uiState.visibile.observeAsState(false)
        Column(
            modifier = androidx.compose.ui.Modifier
                .fillMaxSize(),
            verticalArrangement = Arrangement.Bottom,
            horizontalAlignment = Alignment.CenterHorizontally
        ) {
            AnimatedVisibility(visible = visible) {
                Surface(
                    color = Color.Red, modifier = Modifier
                        .width(100.dp)
                        .height(100.dp)
                ) {

                }
            }
            Text(text = "Hello $content")
            Button(onClick = {
                index++
                content = "change $index"
                uiState.visibile.value = !uiState.visibile.value!!
            }) {
                Text(text = "点击")
            }

            var state by remember {
                mutableStateOf(0)
            }
            AnimatedContent(targetState = state, transitionSpec = {
                slideInHorizontally { width-> -2*width }  with
                        slideOutHorizontally { width-> width } using
                        SizeTransform(clip = false)
            }) {
                Button(onClick = {
                    navigator.push("/text") {
                        launchSingleTop = true
                    }
                    state += 1
                }) {
                    Text(text = "TextScreen")
                }
            }

            Button(onClick = {
                viewModel.sentIntent(MainIntent.TestIntent)
            }) {
                Text(text = "TextScreen")
            }

        }
    }
}

@Preview
@Composable
fun HomeScreenPreview() {
    HomePage()
}

